package com.fzk.blog.blog.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.fzk.blog.bean.vo.BlogInfo;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.settings.service.IBlogService;
import com.fzk.blog.utils.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author fzk
 * @date 2021-08-22 20:35
 */
@RestController
public class BlogController_blog {
    @Resource
    private IBlogService blogService;

    @GetMapping("/blog/info/page")
    public Map<String, Object> getBlogInfoPage(
            @RequestParam("page_no") int pageNo,
            @RequestParam("page_size") int pageSize,
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "type_id", required = false) Integer typeId,
            @RequestParam(name = "user_id", required = false) Integer userId,
            @RequestParam(name = "tag", required = false) Integer[] tag,
            @RequestParam(name = "published", required = false) Boolean published,
            @RequestParam(name = "flag", required = false) Integer flag
    ) {
        // 1.参数检查
        if (tag == null)
            tag = new Integer[]{};
        if (published == null)
            published = true;//默认查发布了的博客
        if (flag == null)
            flag = 0;//默认查未被删除的
        if (pageNo < 0 || pageSize <= 0)
            throw new MyException(400, "参数错误：page_no:" + pageNo + ";page_size:" + pageSize);

        // 2.计算index
        int index = (pageNo - 1) * pageSize;

        // 3.调用service
        Map<String, Object> map = blogService.getBlogInfoPage(title, typeId, userId, tag, published, flag, index, pageSize);

        // 4.返回
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }

    @PutMapping(path = "/blog/viewTimes")
    public Map<String, Object> increaseViewTimes(
            @RequestParam("id") int blogId
    ) {
        int viewTimes = blogService.increaseViewTimes(blogId);
        return ResponseResult.success(null, viewTimes);
    }

    @GetMapping(path = "/blog/newBlogList")
    public Map<String, Object> getNewBlogList() {
        return blogService.getNewBlogInfoList();
    }
}
